package tech.aistar.controller;

import tech.aistar.entity.Phone;
import tech.aistar.entity.PhoneType;
import tech.aistar.service.IPhoneService;
import tech.aistar.service.IPhoneTypeService;
import tech.aistar.service.impl.PhoneServiceImpl;
import tech.aistar.service.impl.PhoneTypeServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author: lya
 * @Description: 本类的作用描述:
 * @CreateDate: 2019/9/9
 * @Version: 1.0
 */
@WebServlet("/phone/update")
public class PhoneUpdateAction extends HttpServlet {
    private IPhoneService phoneService = new PhoneServiceImpl();
    private IPhoneTypeService phoneTypeService = new PhoneTypeServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //修改
        String id = req.getParameter("id");
        Phone phone = phoneService.findById(Integer.valueOf(id));
        PhoneType phoneType = phoneTypeService.findById(phone.getTypeId());
        req.getSession().setAttribute("phone",phone);
        req.getSession().setAttribute("phoneType",phoneType);


        String typeId = req.getParameter("typeId");
        String name = req.getParameter("name");
        String price = req.getParameter("price");
        String url = req.getParameter("url");
        String intro = req.getParameter("intro");
        String free = req.getParameter("free");


        phone.setTypeId(Integer.valueOf(typeId));
        phone.setName(name);
        phone.setPrice(Double.valueOf(price));
        phone.setUrl(url);
        phone.setIntro(intro);
        phone.setFree(Integer.valueOf(free));
        phone.setStatus(1);
        phoneService.updateById(phone);

        resp.sendRedirect(req.getContextPath()+"/phone/index");


    }
}
